﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jalal.Library.Data;
using System.Data.Common;
using Jalal.Library.Utility;

namespace BCGC.DAL
{
    public class SettingsData
    {
        public static DataTable GetSettingsData()
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetSettings";
            return dataMiner.GetListData(command);
        }

        public static int UpdateSettingsData(string adminUsername, string adminPassword, string adminEmail, 
            string supportEmail, string mailServer, string mailUsername, string mailPassword, 
            int mailPort, bool useSSLMail, string encryptionKey, string siteName, string siteUrl, 
            string companyEmail, string companySiteUrl)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdateSettings";
            dataMiner.AddParameter(command, "@AdminUsername", DbType.String, 50, ParameterDirection.Input, adminUsername);
            dataMiner.AddParameter(command, "@AdminPassword", DbType.String, 500, ParameterDirection.Input, adminPassword);
            dataMiner.AddParameter(command, "@AdminEmail", DbType.String, 50, ParameterDirection.Input, adminEmail);
            dataMiner.AddParameter(command, "@SupportEmail", DbType.String, 500, ParameterDirection.Input, supportEmail);
            dataMiner.AddParameter(command, "@MailServer", DbType.String, 500, ParameterDirection.Input, mailServer);
            dataMiner.AddParameter(command, "@MailUsername", DbType.String, 50, ParameterDirection.Input, mailUsername);
            dataMiner.AddParameter(command, "@MailPassword", DbType.String, 50, ParameterDirection.Input, mailPassword);
            dataMiner.AddParameter(command, "@MailPort", DbType.Int32, 0, ParameterDirection.Input, mailPort);
            dataMiner.AddParameter(command, "@UseSSLMail", DbType.Boolean, 0, ParameterDirection.Input, useSSLMail);
            dataMiner.AddParameter(command, "@EncryptionKey", DbType.String, 500, ParameterDirection.Input, encryptionKey);
            dataMiner.AddParameter(command, "@SiteName", DbType.String, 500, ParameterDirection.Input, siteName);
            dataMiner.AddParameter(command, "@SiteUrl", DbType.String, 500, ParameterDirection.Input, siteUrl);
            dataMiner.AddParameter(command, "@CompanyEmail", DbType.String, 500, ParameterDirection.Input, companyEmail);
            dataMiner.AddParameter(command, "@CompanySiteUrl", DbType.String, 500, ParameterDirection.Input, companySiteUrl);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static int CountAdminPasswordMatch(string adminPassword)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_AdminPasswordMatchCount";
            dataMiner.AddParameter(command, "@AdminPassword", DbType.String, 50, ParameterDirection.Input, adminPassword);

            return (int)CommonUtility.FilterNull(dataMiner.GetOneData(command), typeof(int)); 
        }
    }
}
